#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import re
import fullHalfConversion

MaxLength = 1 * 1024 * 1024

def readFile(filename):
    f = open(filename, 'r')
    fileContent = f.read(MaxLength)
    f.close()
    return fileContent

def main(argv=None):
    if argv == None:
        argv = sys.argv

    if len(argv) != 3:
        print "Usage:./insertNewLine.py srcFile destFile"
        exit(1)

    fileContent = readFile(argv[1])
    f2h = fullHalfConversion.fullHalfConversion()
    fileContent = f2h.StringH2F(fileContent)
    nl = '\n'
    start = 0
    offset = 63
    while True:
        index = fileContent.find(nl, start)
        if index == -1:
            break

        prog = re.compile('\w| ')
        n = start + offset
        if index > n:
            fileContent = fileContent[:n] + nl + fileContent[n:]
            start = n + 1 
        else:
            start = index + 1
        #print( "%s start at %d, found at index %d" % (nl, start, index) )

    print fileContent
    if os.path.isdir(argv[2]):
        print("%s is directory" % (argv[2]))
        exit(2)
    else:
        f = open(argv[2], 'w')
        f.write(fileContent)
        f.close()


if __name__ == "__main__":
    sys.exit(main())
